package com.bstek;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bstek.urule.Utils;
import com.bstek.urule.runtime.KnowledgePackage;
import com.bstek.urule.runtime.KnowledgeSession;
import com.bstek.urule.runtime.KnowledgeSessionFactory;
import com.bstek.urule.runtime.response.RuleExecutionResponse;
import com.bstek.urule.runtime.service.KnowledgeService;

@RestController
@RequestMapping("rule")
public class RuleController {

	@RequestMapping("/ageRule")
	public RuleExecutionResponse rule(@RequestParam String age) throws IOException {
		// 创建一个KnowledgeSession对象
		KnowledgeService knowledgeService = (KnowledgeService) Utils.getApplicationContext().getBean(KnowledgeService.BEAN_ID);
		KnowledgePackage knowledgePackage = knowledgeService.getKnowledge("胶州/user_score");
		KnowledgeSession session = KnowledgeSessionFactory.newKnowledgeSession(knowledgePackage);

		Map<String, Object> param = new HashMap<>();
		param.put("id", 1);
		param.put("name", "云中");
		param.put("age", 15);
		param.put("sex", 0);
		param.put("score", 0);
		RuleExecutionResponse result = session.fireRules(param);
		System.out.println("得分：" + session.getParameters());
		return result;
	}
}
